public class MySingleList implements Ilist{
    static class ListNode{
        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public int listSize;

    public void CreatList(){
        ListNode node1 = new ListNode(11);
        ListNode node2 = new ListNode(22);
        ListNode node3 = new ListNode(33);
        ListNode node4 = new ListNode(44);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node1;
    }

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;//相当于插入进一个空的链表
        }else{
            node.next = head;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //表示链表为空
        if(head == null) {
            head = node;
            return;
        }
        //找到链表的尾巴
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public void addIndex(int index, int data) throws IndexOutOf{
        ListNode node = new ListNode(data);
        checkIndexOfAdd(index);
        if(index == 0) {
            addFirst(data);
            return;
        }

        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;
        listSize++;
    }
    private ListNode findIndex(int index){
        ListNode cur = head;
        int count = 0;
        while(count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }
    private void checkIndexOfAdd(int index){
        if(index<0 || index>size()){
            throw new RuntimeException("插入的位置不合法,index="+index);
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur1 = head;
        while(cur1 != null){
            if(cur1.val == key){
                return true;
            }
            cur1 = cur1.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            listSize--;
            return;
        }
        ListNode cur = findNode(key);
        if(cur == null) {
            System.out.println("没有你要删除的数据");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
        listSize--;
    }
    private ListNode findNode(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {

    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        while(head != null){
            System.out.print(head.val+" ");
            head = head.next;
        }
        System.out.println();
    }
}
